package com.gxljc.bear.crawler.itaogao.guochengxin;

import java.io.Serializable;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.log4j.Logger;

import com.gxljc.bear.crawler.itaogao.cnstock.CnstockUpdateCrawler;
import com.gxljc.bear.crawler.proxy.ProxyUtil;

/**
 * 国承信update爬虫
 * 
 * @author tanghaitao
 *
 * @since 2021年11月23日
 */
public class GuochengxinUpdateCrawler implements Serializable {

	private static final long serialVersionUID = 1L;
	public static Logger LOG = Logger.getLogger(CnstockUpdateCrawler.class);
	private Boolean proxy = false;
	private GuochengxinSeedCrawler crawler = null;

	public GuochengxinUpdateCrawler(Boolean proxy) {
		crawler = new GuochengxinSeedCrawler(proxy);
		this.proxy = proxy;
	}

	public void crawl() throws Exception {
		List<String> seeds = getSeed();
		if (CollectionUtils.isEmpty(seeds)) {
			LOG.error("page is empty");
			return;
		}
		ProxyUtil.initQueue(proxy);
		crawlNoSpark(seeds);
	}

	public void crawlNoSpark(List<String> seeds) {
		for (String url : seeds) {
			try {
				crawler.crawl(url);
			} catch (Exception e) {
				LOG.error(String.format("crawlNoSpark error,seeds=%s", seeds), e);
			}
		}
	}

	// 构造URL seed
	@SuppressWarnings("rawtypes")
	public List<String> getSeed() throws Exception {
		Map<String, Integer> seeds = new HashMap<String, Integer>();
		seeds.put("http://www.guochengxin.com/today_list/index_%s.shtml", 1);
		List<String> urls = new LinkedList<String>();
		for (Map.Entry seed : seeds.entrySet()) {
			int max = (Integer) seed.getValue();
			String urlFormat = (String) seed.getKey();
			for (int i = 1; i <= max; i++) {
				String url = String.format(urlFormat, i + "");
				urls.add(url);
			}
		}
		return urls;
	}

}